// 矩形面积

package Leetcode;

class Solution_223_1 {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = (ax2 - ax1) * (ay2 - ay1);
        int area2 = (bx2 - bx1) * (by2 - by1);

        int sum = area1 + area2;

        boolean left_top = (ax1 <= bx1 && bx1 <= ax2) && (ay1 <= by2 && by2 <= ay2);
        boolean left_bottom = (ax1 <= bx1 && bx1 <= ax2) && (ay1 <= by1 && by1 <= ay2);
        boolean right_top = (ax1 <= bx2 && bx2 <= ax2) && (ay1 <= by2 && by2 <= ay2);
        boolean right_bottom = (ax1 <= bx2 && bx2 <= ax2) && (ay1 <= by1 && by1 <= ay2);

        // 重叠部分面积
        int height = 0;
        int width = 0;

        if ((left_top && right_top) || (left_bottom && right_bottom))
            width = bx2 - bx1;
        else if ((left_top && !right_top) || (left_bottom && !right_bottom))
            width = ax2 - bx1;
        else if ((!left_top && right_top) || (!left_bottom && right_bottom))
            width = bx2 - ax1;

        if ((left_top && left_bottom) || (right_top && right_bottom))
            height = by2 - by1;
        else if ((left_top && !left_bottom) || (right_top && !right_bottom))
            height = by2 - ay1;
        else if ((!left_top && left_bottom) || (!right_top && right_bottom))
            height = ay2 - by1;
        System.out.println(width);
        System.out.println(height);
        return sum - width * height;
    }
}

class Solution_223_2 {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = (ax2 - ax1) * (ay2 - ay1);
        int area2 = (bx2 - bx1) * (by2 - by1);

        int sum = area1 + area2;

        // 如果没有并集
        if (!(ay1 <= by2 && by2 <= ay2) && !(ay1 <= by1 && by1 <= ay2) && !(ax1 <= bx1 && bx1 <= ax2)
                && !(ax1 <= bx2 && bx2 <= ax2))
            return sum;

        int height = ay2 - ay1;
        int width = ax2 - ax1;

        if ((ay1 <= by2 && by2 <= ay2) && (ay1 <= by1 && by1 <= ay2)) {
            height = by2 - by1;
        } else if ((ay1 <= by2 && by2 <= ay2) && (by1 < ay1)) {
            height = by2 - ay1;
        } else if ((by2 > ay2) && (ay1 <= by1 && by1 <= ay2))
            height = ay2 - by1;

        if ((ax1 <= bx1 && bx1 <= ax2) && (ax1 <= bx2 && bx2 <= ax2)) {
            width = bx2 - bx1;
        } else if ((ax1 <= bx1 && bx1 <= ax2) && (bx2 > ax2)) {
            width = ax2 - bx1;
        } else if ((bx1 < ax1) && (ax1 <= bx2 && bx2 <= ax2)) {
            width = bx2 - ax1;
        }

        return sum - width * height;
    }
}

class Solution_223_3 {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = (ax2 - ax1) * (ay2 - ay1);
        int area2 = (bx2 - bx1) * (by2 - by1);

        int height = Math.min(by2, ay2) - Math.max(by1, ay1);
        int width = Math.min(bx2, ax2) - Math.max(bx1, ax1);

        return area1 + area2 - Math.max(width,0) * Math.max(height,0);
    }
}